﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quocity.Entities;
using System.Xml.Serialization;
using System.IO;
using PlainConcepts.WCF.Extensions.Cache;

namespace QuocityService
{
    public class XmlPointOfInterestSource:IPointOfInterestSource
    {
        private string city;

        public string City
        {
            get { return city; }
            protected set { city = value; }
        }
        

        public XmlPointOfInterestSource(string city)
        {
            this.City = city;
        }
        
        private List<PointOfInterest> ObtenerArrayOfPointOfInterest()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<PointOfInterest>));
            using (StreamReader streamReader = new StreamReader(HttpContext.Current.Server.MapPath(String.Format(@"~/Sources/{0}.xml",city))))
            {
                List<PointOfInterest> list = serializer.Deserialize(streamReader) as List<PointOfInterest>;
                return list;
            }
            
        }

        public List<Quocity.Entities.PointOfInterest> GetList(string filter, int page, int pageSize)
        {
           
            var result = (from pointOfInterest in ObtenerArrayOfPointOfInterest()
                          where String.IsNullOrEmpty(filter) || pointOfInterest.Name.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase)>=0
                          select pointOfInterest).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return result;
        }
    }
}